<template>
  <div class="my-container">
    <div class="login-info" v-if='user'>
      <div class="header-bg">
        <div class="userinfo">
          <div class='userTouxiang'>
            <van-image :src="userObj.photo" round fit='cover' />
            <span>{{userObj.name}}</span>
          </div>
          <van-button round>编辑资料</van-button>
        </div>
        <div class="userData">
          <div class="data_item">
            <span>{{userObj.art_count}}</span>
            <span>头条</span>
          </div>
          <div class="data_item">
            <span>{{userObj.follow_count }}</span>
            <span>关注</span>
          </div>
          <div class="data_item">
            <span>{{userObj.fans_count}}</span>
            <span>粉丝</span>
          </div>
          <div class="data_item">
            <span>{{userObj.like_count}}</span>
            <span>获赞</span>
          </div>
        </div>
      </div>
      <div class="grid-nav">
        <van-grid :column-num="2">
          <van-grid-item>
            <i slot='icon' class='icon icon-shoucang'></i>
            <span slot='text'>收 藏</span>
          </van-grid-item>
          <van-grid-item>
            <i slot='icon' class='icon icon-lishi'></i>
            <span slot='text'>历 史</span>
          </van-grid-item>
        </van-grid>
      </div>
    </div>
    <div class="header header-bg" v-else>
      <img class="mobile-img" src="~@/assets/mobile.png" @click="$router.push('/login')">
      <span>登录 / 注册</span>
    </div>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell class="logout-cell" title="退出登录" center @click="onLoginOut" v-if='user' />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyIndex',
  components: {},
  props: {},
  data () {
    return {
      userObj: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    if (this.user) this.loadUser()
  },
  mounted () { },
  methods: {
    // 获取用户列表
    async loadUser () {
      try {
        const { data: { data } } = await getUserInfo()
        this.userObj = data
        console.log(this.userObj)
      } catch (err) {
        this.toast('获取失败！')
      }
    },
    // 退出登录
    onLoginOut () {
      this.$dialog.confirm({
        title: '确认退出吗？'
      })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style scoped lang="less">
.my-container {
  .cell-box {
    margin: 10px 0;
  }
  .login-info {
    .userTouxiang {
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      span {
        margin-left: 15px;
        font-size: 35px;
        font-weight: 700;
      }
    }
    .userData {
      height: 130px;
      display: flex;
      justify-content: space-around;
      .data_item {
        color: #fff;
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
    .userinfo {
      height: 247px;
      display: flex;
      padding: 0 32px;
      justify-content: space-between;
      align-items: center;
      .van-image {
        width: 132px;
        height: 132px;
      }
      .van-button {
        width: 100px;
        border: 0;
        height: 48px;
        .van-button__text {
          font-size: 20px;
          white-space: nowrap;
        }
      }
    }
    .grid-nav {
      margin-bottom: 10px;
      .van-grid-item {
        .icon {
          font-size: 0.6rem;
        }
        .icon-shoucang {
          color: #eb5253;
        }
        .icon-lishi {
          color: #ff9d1d;
        }
        span {
          font-size: 28px;
          margin-top: 16px;
        }
      }
    }
  }
  .header-bg {
    height: 401px;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
  }
  .header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
    .mobile-img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      margin-top: 15px;
      color: #fff;
    }
  }
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    // margin: 10px 0;
    font-size: 30px;
    margin-top: 10px;
  }
}
</style>
